package lambda;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author : 李红磊
 * @version :1.0
 * @date : 2021/10/9 14:42
 * @description :   Stream练习
 */
public class Demo2 {

    /*
     *
     * 给定一个数字列表，返回一个由这个列表中每个元素平方构成的新列表
     *
     * */
    @Test
    public void test1() {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(5);
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(9);

        list.stream()
                .map((x) -> x * x)//中间操作
                .forEach(System.out::println);//终止操作

    }

    /*
     * 使用map和reduce来判断流中有多少个student
     *
     * */
    @Test
    public void test2(){

        List<Student> students = Arrays.asList(new Student("朱元璋", 64, 1.76f),
                new Student("朱允炆", 24, 1.76f),
                new Student("朱棣", 24, 1.76f),
                new Student("朱高炽", 24, 1.76f),
                new Student("朱瞻基", 24, 1.76f),
                new Student("朱祁镇", 24, 1.76f),
                new Student("朱祁钰", 24, 1.76f),
                new Student("主见深", 24, 1.76f),
                new Student("朱佑樘", 24, 1.76f),
                new Student("朱厚照", 24, 1.76f),
                new Student("朱厚熜", 24, 1.76f),
                new Student("朱载垕", 24, 1.76f),
                new Student("朱翊钧", 24, 1.76f),
                new Student("朱常洛", 24, 1.76f),
                new Student("朱由校", 24, 1.76f),
                new Student("朱由检", 14, 1.76f));

        Optional<Integer> count = students.stream()
                .map((x) -> 1)
                .reduce(Integer::sum);
        System.out.println(count.get());



    }


}
